//
//  TouchExampleController.m
//  test
//
//  Created by 科迈捷 on 2017/3/28.
//  Copyright © 2017年 科迈捷. All rights reserved.
//

#import "TouchExampleController.h"
#import "TransitionAnimator.h"

@interface TouchExampleController ()<UIViewControllerTransitioningDelegate> {
    CGPoint _touchPoint;
}
@property (nonatomic, strong) UIButton *backBtn;
@end

@implementation TouchExampleController

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _backBtn.backgroundColor = [UIColor redColor];
        [_backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [_backBtn setTitle:@"BACK" forState:UIControlStateNormal];
    }
    return _backBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    switch (self.type) {
        case 0:
        {
            [self startKeyframeAnimation];
        }
            break;
        case 1:
        {
            [self startCustomTransitionAnimation];
        }
            break;
        case 2:
        {
            [self startCustomControllerTransitionAniamtion1:touches.anyObject];
        }
            break;
        default:
            break;
    }
}

- (void)setup {
    switch (self.type) {
        case 1:
        {
            self.view.layer.masksToBounds = YES;
            self.view.layer.contentsGravity = @"resizeAspectFill";
            self.view.layer.contents = (id)[UIImage imageNamed:@"1"].CGImage;
        }
            break;
        case 2:
        {
            self.view.layer.masksToBounds = YES;
            self.view.layer.contentsGravity = @"resizeAspectFill";
            self.view.layer.contents = (id)[UIImage imageNamed:@"1"].CGImage;
        }
            break;
        case 3:
        {
            self.view.layer.masksToBounds = YES;
            self.view.layer.contentsGravity = @"resizeAspectFill";
            self.view.layer.contents = (id)[UIImage imageNamed:@"2"].CGImage;
            self.backBtn.frame = CGRectMake(50, 50, 100, 40);
            [self.view addSubview:self.backBtn];
        }
        default:
            break;
    }
}

#pragma mark - 
- (void)startKeyframeAnimation {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"backgroundColor";
    animation.values = @[(id)[UIColor whiteColor].CGColor,(id)[UIColor redColor].CGColor,(id)(id)[UIColor blueColor].CGColor,(id)[UIColor blackColor].CGColor,(id)[UIColor whiteColor].CGColor];
    animation.keyTimes = @[@0,@0.2,@0.5,@0.7,@1];
    animation.duration = 5;
    animation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.view.layer addAnimation:animation forKey:nil];
}

- (void)startCustomTransitionAnimation {
    //获取截图
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *coverImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //将截图添加到主视图
    UIImageView *coverImageView = [[UIImageView alloc]initWithImage:coverImage];
    coverImageView.frame = self.view.bounds;
    [self.view addSubview:coverImageView];
    //更换背景图片
    self.view.layer.contents = (id)[UIImage imageNamed:@"2"].CGImage;
    //添加动画
    [UIView animateWithDuration:1 animations:^{
        CGAffineTransform transform = CGAffineTransformMakeScale(0.01, 0.01);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        coverImageView.alpha = 0;
        coverImageView.transform = transform;
    } completion:^(BOOL finished) {
        [coverImageView removeFromSuperview];
    }];
}

- (void)startCustomControllerTransitionAniamtion1:(UITouch *)touch {
    _touchPoint = [touch locationInView:self.view];
    
    TouchExampleController *vc = [TouchExampleController new];
    vc.type = 3;
    vc.transitioningDelegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark - animation
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    TransitionAnimator *animator = [TransitionAnimator new];
    animator.touchPoint = _touchPoint;
    return animator;
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
